package main

import (
	"fmt"
)

func main() {
	arr := [...]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

	fmt.Println("arr[2:6] = ", arr[2:6])
	fmt.Println("arr[:6] = ", arr[:6])
	fmt.Println("arr[2:] = ", arr[2:])
	fmt.Println("arr[:] = ", arr[:])

	s1 := arr[:6]
	s2 := arr[2:]
	updateSlice(s1)
	updateSlice(s2)

	fmt.Println(s1, s2, arr)

	fmt.Println("reSlice的结果")
	s2 = s2[2:]
	fmt.Println(s2, arr)
	s2 = s2[:3]
	fmt.Println(s2, arr)
	s2 = s2[:]
	fmt.Println(s2, arr)

	fmt.Println("slice超界取值的结果")
	sliceDemo()

	fmt.Println("======== 操作 slice =========")
	sliceOp()
}

func sliceOp() {
	//创建
	var s []int
	for i := 0; i < 100; i++ {
		s = append(s, i)
	}
	fmt.Println(s)

	s2 := make([]int, 16)
	s3 := make([]int, 10, 32)
	fmt.Println(s2, s3)
	fmt.Println("=======复制======")
	copy(s2, s)
	printSlice(s2)
	fmt.Println("=======删除元素======")
	s2 = append(s2[:3], s2[4:]...)
	printSlice(s2)
	fmt.Println("=======pop from head ======")
	front := s2[0]
	s2 = s2[1:]
	fmt.Println("pop from head", front)
	printSlice(s2)
	fmt.Println("=======pop from tail ======")
	end := s2[len(s2)-1]
	s2 = s2[:len(s2)-1]
	fmt.Println("pop from tail", end)
	printSlice(s2)

}

func printSlice(s []int) {
	fmt.Printf("value:%v len:%d cap:%d \n", s, len(s), cap(s))
}
func updateSlice(s []int) {
	s[0] = 100
}

func sliceDemo() {
	arr := [...]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
	s1 := arr[2:6]
	fmt.Printf("s1=%v, len(s1)=%d, cap(s1)=%d\n", s1, len(s1), cap(s1))
	s2 := s1[0:7]
	s1 = s1[0:8]
	fmt.Println(s1, s2)
}
